# TOP Makefile

BUILD_INFO := =============== Build OK!! Author: JinYiCheng  ===============

KBUILD_VERBOSE ?=
ifeq ("$(origin DEBUG)", "command line")
  KBUILD_VERBOSE = -g
endif
ifndef KBUILD_VERBOSE
  KBUILD_VERBOSE =
endif


target = m4_base

executable_suffix :=

# top Makefile directory
makefile_dir = $(CURDIR)

# debug build?
build_for_debug = 1

# build for ARM?
build_for_arm = 1

# designate gcc compiler path when without env
compiler_path ?= 

# cross compiler prefix name
ifneq ($(build_for_arm),0)
cross_compile ?= arm-none-eabi-
endif

# Build path
build_dir = $(makefile_dir)/build

# lib path
lib_dir = lib

# input encode type GB2312 or UTF-8
input_encode =

# output encode type GB2312 or UTF-8
ouptput_encode =

# The gcc compiler bin path can be either defined in make command via GCC_PATH variable (> make GCC_PATH=xxx)
ifdef compiler_path
CC = $(compiler_path)/$(cross_compile)gcc
CXX = $(compiler_path)/$(cross_compile)g++
CP = $(compiler_path)/$(cross_compile)objcopy
OD = $(compiler_path)/$(cross_compile)objdump
LD = $(compiler_path)/$(cross_compile)ld
AR = $(compiler_path)/$(cross_compile)ar
SZ = $(compiler_path)/$(cross_compile)size
else
CC = $(cross_compile)gcc
CXX = $(cross_compile)g++
CP = $(cross_compile)objcopy
OD = $(cross_compile)objdump
LD = $(cross_compile)ld
AR = $(cross_compile)ar
SZ = $(cross_compile)size
endif

#将问号改成冒号就出问题，why
obj_files ?= $(patsubst %.c, $(build_dir)/%.o, $(notdir $(c_srcfiles)))
obj_files += $(patsubst %.s, $(build_dir)/%.o, $(notdir $(asm_srcfiles)))

# ASM files directory
asm_dir := ./arch

# C source files directory
csrc_dir := ./user
include_dir := 

csrc_dir += 
include_dir += 
			
link_library_dir := 
link_library := 

link_library_dir +=
link_library += 
#静态库
link_path ?= $(addprefix -L,$(link_library_dir))#静态库路径
link_flags := $(link_path) $(addprefix -l,$(link_library)) #静态库名称

ifneq ($(build_for_debug),0)
c_flags += -Wall -O0
else
c_flags += -O0
endif

c_srcfiles := $(foreach dir, $(csrc_dir), $(wildcard $(dir)/*.c))
#search C files in these dirs
vpath %.c $(sort $(dir $(c_srcfiles)))

asm_srcfiles ?= $(foreach dir, $(asm_dir), $(wildcard $(dir)/*.s))
asm_srcfiles += $(foreach dir, $(asm_dir), $(wildcard $(dir)/*.S))
#search ASM files in these dirs
vpath %.s $(sort $(dir $(asm_srcfiles)))

c_flags += $(ouptput_encode) $(input_encode)
cpu = -mcpu=cortex-m4 -mthumb
# add c file header path
c_flags += $(cpu) $(addprefix -I,$(include_dir))

s_flags := $(c_flags)

all : $(build_dir) $(build_dir)/$(target).bin debug_info

#非交叉编译一般不需要链接脚本?
#link_script记得要添加路径
link_script ?= link.lds
# link_script ?= -Ttext 0x87800000 -Tdata 0x87800200 -Tbss 0x87800300
# link_script ?= -Ttext 0x87800000
#不链接标准库文件，通常用于编译内核 uboot
link_options := -Map=$(build_dir)/$(target).map

$(build_dir)/$(target).bin : $(obj_files)
#$(filter $(build_dir)/start.o,$(obj_files)) $(filter-out $(build_dir)/start.o,$(obj_files))可以替代$^用来指定链接顺序
	$(LD) -T$(link_script) $(link_options) -o $(build_dir)/$(target).elf $(link_flags) $^
	$(CP) -O binary -S $(build_dir)/$(target).elf $@
	$(CP) -O ihex $(build_dir)/$(target).elf $(build_dir)/$(target).hex
	$(OD) -D -m arm $(build_dir)/$(target).elf > $(build_dir)/$(target).dis
	$(SZ) $(build_dir)/$(target).elf

$(build_dir)/%.o : %.s
	$(CC) $(s_flags) -nostdlib -c -o $@ $<

$(build_dir)/%.o : %.c
	$(CC) $(c_flags) -nostdlib -c -o $@ $<

$(build_dir) :
	mkdir $@

$(lib_dir) :
	mkdir $@

debug_info:
	@echo $(BUILD_INFO)

.PHONY: clean echo

echo:
	@echo "build directory:" $(build_dir)
	@echo $(MAKE_VERSION)
	@echo $(origin DEBUG)
	@echo $(obj_files)
	@echo $(build_dir)
	@echo $(c_srcfiles)
	@echo $(asm_srcfiles)
	@echo $(include_dir)
	@echo $(c_flags)
	@echo $(s_flags)
	@echo $(link_flags)
	@echo $(CC)
	@echo $(LD)
	@echo $(CP)
clean:
	-rm -fr $(build_dir)/*.o $(build_dir)/*.elf $(build_dir)/*.bin $(build_dir)/*.dis